package cn.sumpu.app.coolsk.biz.impl;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
//import org.springframework.transaction.annotation.Transactional;

import cn.sumpu.app.coolsk.biz.AuxTestBiz;
import cn.sumpu.app.coolsk.dao.AuxTestDao;
import cn.sumpu.app.coolsk.domain.AuxTest;
import cn.sumpu.app.coolsk.exception.AppBusinessException;

public class AuxTestBizImpl implements AuxTestBiz {

	private AuxTestDao auxTestDao;

	private DataSourceTransactionManager transactionManager;

	public void settransactionManager(
			DataSourceTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}

	public void setAuxTestDao(AuxTestDao auxTestDao) {
		this.auxTestDao = auxTestDao;
	}

	public AuxTest test(AuxTest test) throws AppBusinessException {
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();// 事务定义类
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);// 设置事务传播属性
		TransactionStatus status = transactionManager.getTransaction(def);// 返回事务对象
		try {
			auxTestDao.deleteAll();
			auxTestDao.insert(test);
			AuxTest ret = auxTestDao.query(test);
			AuxTest updateObject = new AuxTest();
			updateObject.setMsg(test.getMsg() + " for updated");
			auxTestDao.update(updateObject);
			auxTestDao.delete(updateObject);
			transactionManager.commit(status);// 提交事务
			return ret;
		} catch (Exception e) {
			transactionManager.rollback(status);// 回滚事务
			throw new AppBusinessException(e);
		}
	}

}
